package cn.com.zpark;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @PackageName: cn.com.zpark
 * @ClassName: ServletAJAXJSON
 * @Description:
 * @author: RZS
 * @date: 2021/12/23  17:44
 */
@WebServlet("/ajax.do")
public class ServletAJAXJSON extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter w = response.getWriter();

        // 创建json创建对象
        JSONObject obj = new JSONObject();
        // 将需要转化的字符添加到JSONObject对象中
        obj.put("name", "张三");
        obj.put("data", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        obj.put("array", new String[]{"str", "msg"});
        obj.put("row", 255);
        obj.put("Null", null);
        obj.put("bool", true);

        // 转为json格式
        String json = obj.toString();
        System.out.println(json);

        w.write(json);
    }
}
